#!/usr/bin/perl
#lvcreate.cgi
require './lvm-lib.pl';
&ReadParse();
my ($err_msg,$found,$lv,$vgname,@vginfo,@lvinfo,@ssinfo,$v,$vg,$l,$ll);
$lv = $in{'lv'};


@vginfo = get_vg_info();
@lvinfo = get_lv_info();
@ssinfo = get_snapshot_info();
$lv =~ /dev\/(\S+)\/(\S+)/;
$vgname = $1;
$origin = $2;
$origin =~ /volume(\d+)/;
$origid = $1;

$found = 0;
foreach $v (@vginfo) {
	if($v->{'vg'} eq $vgname){
		$vg = $v;
		$found = 1;
		$last;
	}
}
if(!$found){
	$err_msg = $text{'err_vgnofound'};
	goto ERROR_OUT;
}
if($vg->{'free'} eq 0){
	$err_msg = $text{'err_vg_nofreesize'};
	goto ERROR_OUT;
}

foreach $l (@lvinfo) {
	if($l->{'lv'} eq $lv) {
		$ll = $l;
		last;
	}
}
my $sssize = int($ll->{'usedsize'} * 1.2);
if($sssize < 100){
	$sssize = 100;
}
#$err_msg = $ll->{'size'}."|".$ll->{'freesize'}."|".$usedsize."|".$sssize;
#goto ERROR_OUT;
my $n = 1;
foreach $s (@ssinfo) {
	if ($s->{'originlv'} eq $ll->{'lvname'}) {
		$ss = $s;
		$n++;
		next;
	}
	else{
		next;
	}
}
$snaplvname = "v".$origid."snapshot".$n;

$err_msg = $snaplvname;
#goto ERROR_OUT;

&header($text{'title_sscreate'}, "", "crt_snapshot", undef, 0);
print "<form name=form_lvcreate action=index.cgi method=post>\n";
print "<table class=mode1 width=80%>";
print "<tr><td width=150><b>$text{'index_vg'}</b></td><td>$vg->{'vg'}</td></tr>\n";
#print "<tr><td width=150><b>$text{'max_size'}</b></td><td>$vg->{'free'}</td></tr>\n";
print "<input type=hidden name=vgfree value=$vg->{'free'}>";
print "<tr><td width=150><b>$text{'originlv1'}</b></td><td>$origin</td></tr>\n";
print "<tr><td width=150><b>$text{'snapname'}</b></td><td>$snaplvname</td></tr>\n";
print "<tr><td><b>$text{'minsize'}</b></td><td>$sssize</td></tr>\n";
print "<tr><td width=150><b>$text{'max_size'}</b></td><td>$vg->{'free'}</td></tr>\n";
my $g = "GB";
my $m = "MB";
print "<tr><td width=150><b>$text{'ss_size'}</b></td>";
print "<td><input name=lvsize class=text size=10 value=$sssize> ";
print "<select name=unit><option selected value=$m>$m<option value=$g>$g</select>";
print "</td></tr>\n";
print "</table>\n";

print "<table border=0 class=mode1>\n";
print "<tr><td>";
print "<input type=submit name=create class=button1 value=$text{'bt_create'} onclick='return SetEditValue(this)'>\n"; 
print "</td>";
print "<td><input type=submit name=cancel class=button1 value=$text{'bt_cancel'}></td>";
print "</tr>\n";
print "</table>\n";
print "<input type=hidden name=origlv value=$ll->{'lv'}>\n";
print "<input type=hidden name=snapname value=$snaplvname>\n";
print "<input type=hidden name=minsize value=$sssize>\n";
print "<input type=hidden name=maxsize value=$vg->{'free'}>\n";
print <<EOF;
<script>
function SetEditValue(obj)
{
        if (obj == document.form_lvcreate.create) {
                document.form_lvcreate.action = 'act_sscreate.cgi';
        }
}
</script>
EOF
print "</form>\n";
&footer("/lvm", $text{'index'});
return;
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
